package com.game.logic.items.account;

import com.coment.cfg.luban.constant.CurrencyType;
import com.game.framework.manager.core.ActorDaoAble;
import com.coment.po.UserAccount;
import com.coment.message.account.PayInfo;
import com.coment.message.account.ReceiveInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/// 账户演员
@Slf4j
public class UserAccountActor extends ActorDaoAble {
    private final Integer userInfoId;
    private UserAccount userAccount;

    public UserAccountActor(Integer userInfoId) {
        this.userInfoId = userInfoId;
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(PayInfo.class, this::handlePay)
                .match(ReceiveInfo.class, this::handleReceive)
                .build();
    }

    /// 处理支付
    private void handlePay(PayInfo payInfo) {
        this.userAccount = userAccountDao.findByUserInfoIdOrCreate(userInfoId);
        Integer consumerId = payInfo.getConsumerId();

        int type = payInfo.getCurrencyType();
        double value = payInfo.getValue();

        // 默认成功
        payInfo.setPay(true);

        // 校验
        if (!Objects.equals(consumerId, userInfoId)) {
            payInfo.setPay(false);
            payInfo.setMessage("支付用户id错误");
            this.sender().tell(payInfo, self());
        }

        if (value < 0) {
            payInfo.setPay(false);
            payInfo.setMessage("支付金额不能小于0");
        }

        if (type < 0 || type >= CurrencyType.total) {
            payInfo.setPay(false);
            payInfo.setMessage("支付类型错误");
        }

        if (this.userAccount.currency[type] < value) {
            payInfo.setPay(false);
            payInfo.setMessage("%s 支付金额不足 have %s need %s".formatted(
                    payInfo.getConsumerId(),
                    this.userAccount.currency[type],
                    value
            ));
        }

        if (payInfo.isPay()) {
            this.userAccount.currency[type] -= value;
            userAccountDao.save(userAccount);
        }

        getSender().tell(payInfo, getSelf());
    }

    /// 处理收款
    private void handleReceive(ReceiveInfo receiveInfo) {
        this.userAccount = userAccountDao.findByUserInfoIdOrCreate(userInfoId);
        Integer receiverId = receiveInfo.getReceiverId();

        int type = receiveInfo.getCurrencyType();
        double value = receiveInfo.getValue();

        // 默认成功
        receiveInfo.setReceive(true);

        // 校验
        if (!Objects.equals(receiverId, userInfoId)) {
            receiveInfo.setReceive(false);
            receiveInfo.setMessage("收款用户id错误");
            this.sender().tell(receiveInfo, self());
            return;
        }

        if (value < 0) {
            receiveInfo.setReceive(false);
            receiveInfo.setMessage("收款金额不能小于0");
            this.sender().tell(receiveInfo, self());
            return;
        }

        if (type < 0 || type >= CurrencyType.total) {
            receiveInfo.setReceive(false);
            receiveInfo.setMessage("货币类型错误");
            this.sender().tell(receiveInfo, self());
            return;
        }

        // 执行收款操作
        if (receiveInfo.isReceive()) {
            this.userAccount.currency[type] += value;
            userAccountDao.save(userAccount);
        }

        log.info("收款成功 {} {} {}", receiverId, type, value);


        getSender().tell(receiveInfo, getSelf());
    }
}
